package com.zhao.modleclass.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.zhao.modleclass.Helper.BitmapCircleTransformation;
import com.zhao.modleclass.R;
import com.zhao.modleclass.bean.LiveActivityBean;
import com.zhao.modleclass.interfaces.IViewPager;
import com.zhao.modleclass.utils.Constant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.zhao.modleclass.R.id.imageView_show_item_play_movie;

/**
 * Created by 张博文 on 2016/11/28.
 */

public class Live_Fragment_Activity extends AppCompatActivity {
    private Context mContext = this;
    private List<LiveActivityBean.DataBean> mList = new ArrayList<LiveActivityBean.DataBean>();
    private int index = 0;
    private rx.Observable<LiveActivityBean> observable;
    //声明一个webview 
    private ImageView iv_item_video_techHeadUrl, iv_item_video_techIconUrl, iv_item_video_share, iv_item_video_zanNum, imageView_show_play_movie, imageView_show_item;
    private TextView tv_item_video_techNickName, tv_item_video_techJobName, tv_item_video_title, tv_item_video_title2, tv_item_video_startTime, tv_item_video_signUpTotal, tv_item_video_supportTotal;
    private VideoView videoView_item_video_movie;
    private WebView mWebView;
    private RadioGroup mRadioGroup;
    private RadioButton rb_item_video_shoucang, rb_item_video_lookhuigu;
    public Drawable shoucang;
    private boolean flag = true;
    private int CourseId;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);
        //设置状态栏的颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            //获取样式中的属性值
            TypedValue typedValue = new TypedValue();
            this.getTheme().resolveAttribute(Color.parseColor("#FF4081"), typedValue, true);
            int[] attribute = new int[]{Color.parseColor("#FF4081")};
            TypedArray array = this.obtainStyledAttributes(typedValue.resourceId, attribute);
            int color = array.getColor(0, Color.parseColor("#FF4081"));
            array.recycle();
            window.setStatusBarColor(color);
        }
        setContentView(R.layout.activity_live_fragment);
        initView();
        initData();

    }


    //加载数据
    private void initData() {
        final Intent intent = getIntent();
        CourseId = intent.getIntExtra("CourseId", 1221);
        Map<String, String> map = new HashMap<>();
        map.put("courseId", CourseId + "");
        map.put("deviceId", "7fb8fd0b59f6358e965b628fae11306e");
//https://mrobot.pclady.com.cn/modern/s/course/videoCourseInfo.xsp?courseId=1242&deviceId=4024b89134ec3dd6818f09e422d2cafa
        //gson解析加载内容
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.Live_itemURL)//加载网址
                .addConverterFactory(GsonConverterFactory.create())//gson解析
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//用Rxjava模式
                .build();//构建
        //拼接网址
        IViewPager iViewPager = retrofit.create(IViewPager.class);
        Observable<LiveActivityBean> observable = iViewPager.getJsonLiveItemString(map);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LiveActivityBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    //加载数据点击事件
                    @Override
                    public void onNext(final LiveActivityBean liveActivityBean) {
                        //加载图片
                        Glide.with(mContext).load(liveActivityBean.getData().getTechHeadUrl())
                                .transform(new BitmapCircleTransformation(mContext))
                                .into(iv_item_video_techHeadUrl);
                        Glide.with(mContext).load(liveActivityBean.getData().getTechIconUrl()).into(iv_item_video_techIconUrl);
                        Glide.with(mContext).load(liveActivityBean.getData().getImageUrl()).into(imageView_show_item);
                        tv_item_video_techNickName.setText(liveActivityBean.getData().getTechNickName());
                        tv_item_video_techJobName.setText(liveActivityBean.getData().getTechJobName());
                        tv_item_video_title.setText(liveActivityBean.getData().getTitle());
                        tv_item_video_title2.setText(liveActivityBean.getData().getTitle());
                        tv_item_video_startTime.setText("开课时间：" + liveActivityBean.getData().getStartTime());
                        tv_item_video_signUpTotal.setText("浏览人数：" + liveActivityBean.getData().getSignUpTotal() + "人");
                        if (liveActivityBean.getData().getCourseUrl() == null) {
                            iv_item_video_zanNum.setVisibility(View.INVISIBLE);   //View.VISIBLE  显示     View.GONE    不占为隐藏     View.INVISIBLE    占位隐藏
                        } else {
                            tv_item_video_supportTotal.setText(liveActivityBean.getData().getSupportTotal() + "人点赞");
                        }
                        //头像点击
                        iv_item_video_techHeadUrl.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String url = "https://mrobot.pclady.com.cn/modern/s/teacher/techInfo.xsp?techId=" + liveActivityBean.getData().getTechId();
                                String name = liveActivityBean.getData().getTechNickName();
                                Bundle bundle = new Bundle();
                                bundle.putString("url", url);
                                bundle.putString("name", name);
                                //intent跳转
                                Intent intent = new Intent(mContext, SC_TeacherWebView.class);
                                intent.putExtra("url", bundle);
                                intent.putExtra("name", bundle);
                                //启动跳转
                                mContext.startActivity(intent);
                            }
                        });

                        //点击播放视频
                        imageView_show_play_movie.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (liveActivityBean.getData().getCourseUrl().size() > 0) {
                                    String urlvideo = liveActivityBean.getData().getCourseUrl().get(0) + "";
                                    Intent intent_Play = new Intent(Live_Fragment_Activity.this, PlayVideoActivity.class);
                                    intent_Play.putExtra("urlvideo", urlvideo);
                                    startActivity(intent_Play);
                                } else {
                                    Toast.makeText(mContext, "敬请期待", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

                        //一键分享
                        iv_item_video_share.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ShareSDK.initSDK(mContext);
                                OnekeyShare oks = new OnekeyShare();
                                //关闭sso授权
                                oks.disableSSOWhenAuthorize();
                                // text是分享文本，所有平台都需要这个字段
                                oks.setText(liveActivityBean.getData().getStartTime() + liveActivityBean.getData().getTechNickName() + "在摩登课堂APP直播【" + liveActivityBean.getData().getTitle() + "】"
                                        + "http://g.pclady.com.cn/modern/course/" + CourseId + ".html?t=14818509231614471040347(来自@pclady-摩登课堂)");
                                //网络图片的url：所有平台
                                oks.setImageUrl(liveActivityBean.getData().getImageUrl());//网络图片rul
                                oks.setUrl("http://sharesdk.cn");   //网友点进链接后，可以看到分享的详情
                                // 启动分享GUI
                                oks.show(mContext);
                            }
                        });
                    }
                });

        //网页网址
        final String url = "https://mrobot.pclady.com.cn/modern/s/live/liveCourse.xsp?courseId=" + CourseId + "&deviceId=7fb8fd0b59f6358e965b628fae11306e&inReview=0&FromPCapp=PCLADY_MODERN_ANDROID_1.0.0&appVersion=1220";
        //加载网址
        mWebView.loadUrl(url);
        //开启javaScript支持
        mWebView.getSettings().setJavaScriptEnabled(true);
        //强制使用控件加载网页
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    //结束ShareSDK的统计功能并释放资源
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK(this);
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_item_video_shoucang:
                //设置底部导航初始和点击后的颜色
//                final int OtherColor = getResources().getColor(R.color.gray);
//                final int SelectColor = getResources().getColor(R.color.yellow);
//                    if (flag){
//                        flag=!flag;
//                        shoucang.setColorFilter(SelectColor, PorterDuff.Mode.SRC_IN);
//                    }else {
//                        shoucang.setColorFilter(OtherColor, PorterDuff.Mode.SRC_IN);
//                    }
                break;
            case R.id.iv_item_video_back:
                finish();
                break;
            case R.id.iv_item_video_home:
//                Intent intent=new Intent(mContext, HomePageFragment.class);
//                startActivity(intent);
                break;
            case R.id.iv_item_video_add:

                break;
            case R.id.rb_item_video_lookhuigu:
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
                break;
        }
    }

    //初始化数据
    private void initView() {
        iv_item_video_techHeadUrl = (ImageView) findViewById(R.id.iv_item_video_techHeadUrl);
        iv_item_video_techIconUrl = (ImageView) findViewById(R.id.iv_item_video_techIconUrl);
        iv_item_video_share = (ImageView) findViewById(R.id.iv_item_video_share);
        iv_item_video_zanNum = (ImageView) findViewById(R.id.iv_item_video_zanNum);
        tv_item_video_techNickName = (TextView) findViewById(R.id.tv_item_video_techNickName);
        tv_item_video_techJobName = (TextView) findViewById(R.id.tv_item_video_techJobName);
        tv_item_video_title = (TextView) findViewById(R.id.tv_item_video_title);
        tv_item_video_title2 = (TextView) findViewById(R.id.tv_item_video_title2);
        tv_item_video_startTime = (TextView) findViewById(R.id.tv_item_video_startTime);
        tv_item_video_signUpTotal = (TextView) findViewById(R.id.tv_item_video_signUpTotal);
        tv_item_video_supportTotal = (TextView) findViewById(R.id.tv_item_video_supportTotal);
        imageView_show_item = (ImageView) findViewById(R.id.imageView_item_movie);
        imageView_show_play_movie = (ImageView) findViewById(imageView_show_item_play_movie);
        videoView_item_video_movie = (VideoView) findViewById(R.id.videoView_item_video_movie);
        mWebView = (WebView) findViewById(R.id.wv_live_video_activity);
    }
}

